<template>
  <div class="app-container">
    <!-- 添加或修改请假业务对话框 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="出差标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入出差标题" />
      </el-form-item>
      <el-form-item label="出差内容" prop="content">
        <el-input v-model="form.content" placeholder="请输入出差内容" />
      </el-form-item>
      <el-form-item label="出差人" prop="triper">
        <el-input v-model="form.triper" placeholder="请输入出差人" />
      </el-form-item>
      <el-form-item label="目的地" prop="address">
        <el-input v-model="form.address" placeholder="请输入目的地" />
      </el-form-item>
      <el-form-item label="时长" prop="days">
        <el-input v-model="form.days" placeholder="请输入时长" />
      </el-form-item>
      <el-form-item label="开始日期" prop="startTime">
        <el-date-picker clearable
                        v-model="form.startTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endTime">
        <el-date-picker clearable
                        v-model="form.endTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否审核" prop="status">
        <el-select v-model="form.status" placeholder="请选择是否审核" disabled="true">
          <el-option
            v-for="dict in dict.type.bs_trip_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
      <div slot="footer" v-show="parentTaskId===null" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="info" @click="submitForm">暂存</el-button>
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">提交</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
      <verify ref="verifyRef" :taskId="taskId" @callSubmit="callSubmit"
      :taskVariables="taskVariables" :sendMessage="sendMessage"></verify>
  </div>
</template>

<script>
import {getTrips,updateTrips} from "@/api/demo/trips";
import processAip from "@/api/workflow/processInst";
import verify from "@/components/Process/Verify";
export default {
  name: "TripsFrom",
  dicts: ['bs_trip_status'],
  props: {
      businessKey: String, // 业务唯一标识
      parentTaskId: String, // 父级任务id
      taskId: String // 任务id
  },
  components: {
      verify
    },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 请假业务表格数据
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "出差标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "出差内容不能为空", trigger: "blur" }
        ],
        triper: [
          { required: true, message: "出差人不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "目的地不能为空", trigger: "blur" }
        ],
        days: [
          { required: true, message: "时长不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始日期不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "是否同意不能为空", trigger: "change" }
        ],
      },
      taskVariables: undefined,
      //消息提醒
      sendMessage: {}
    };
  },
  watch: {
      businessKey: {
          immediate: true, // 很重要！！！
          handler (newVal) {
              if(newVal) {
                  this.getById()
              }
          }
      }
  },
  methods: {
    // 取消按钮
    cancel() {
      this.$emit("closeForm")
    },
    callSubmit(){
      //查询ActBusinessStatus
      processAip.getInfoByBusinessKey(this.businessKey).then(response => {
        if (response!=null && response.data!=null && response.data.status == 'finish') {
          let tripsData = {'id': response.data.businessKey, 'status': '2'}
          updateTrips(tripsData);
        }
        this.$emit("closeForm")
      })

    },
    async getById() {
        const {data} = await getTrips(this.businessKey)
        this.form = data;
    },
    /** 提交按钮 */
    submitForm() {
      getTrips(this.businessKey).then(response => {
            this.taskVariables = {
                 entity: response.data,
                 userId :1
            };
            this.sendMessage = {
              title:'请假申请',
              messageContent:'单据【'+this.form.id+"】申请"
            }
      });
      this.$refs.verifyRef.visible = true
    },
    //提交流程
    submitFormAppply(){
        getTrips(this.form.id).then(response => {
            const data = {
                processKey: 'trip', // key
                classFullName: 'com.ruoyi.demo.leave.domain.BsTrips', // 全类名
                businessKey: response.data.id, // 业务id
                variables: { entity: response.data }, // 变量
            }
            processAip.startProcessApply(data).then(response => {
              if(response.code===200){
                this.$modal.msgSuccess(response.msg);
              }
            })
        });
    }
  }
};
</script>
